home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 12 / Example 12.7 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  6.7 KB  |  260 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 12.7: Constructing Buildings                        //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include <vector>
  9. #include "debug.h"
  10. #include "mesh.h"
  11.  
  12. class APPLICATION
  13. {
  14.     public:
  15.         APPLICATION();
  16.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  17.         HRESULT Update(float deltaTime);
  18.         HRESULT Render();
  19.         HRESULT Cleanup();
  20.         HRESULT Quit();
  21.         DWORD FtoDword(float f){return *((DWORD*)&f);}
  22.  
  23.     private:
  24.         IDirect3DDevice9* m_pDevice; 
  25.         MESH m_build1, m_build2;
  26.         float m_buildPrc;
  27.  
  28.         D3DLIGHT9 m_light;
  29.         HWND m_mainWindow;
  30. };
  31.  
  32. D3DRECT SetRect(long x1, long y1, long x2, long y2)
  33. {
  34.     D3DRECT r;
  35.     r.x1 = x1;
  36.     r.y1 = y1;
  37.     r.x2 = x2;
  38.     r.y2 = y2;
  39.     return r;
  40. }
  41.  
  42. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  43. {
  44.     APPLICATION app;
  45.  
  46.     if(FAILED(app.Init(hInstance, 800, 600, true)))return 0;
  47.  
  48.     MSG msg;
  49.     memset(&msg, 0, sizeof(MSG));
  50.     int startTime = timeGetTime(); 
  51.  
  52.     while(msg.message != WM_QUIT)
  53.     {
  54.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  55.         {
  56.             ::TranslateMessage(&msg);
  57.             ::DispatchMessage(&msg);
  58.         }
  59.         else
  60.         {    
  61.             int t = timeGetTime();
  62.             float deltaTime = (t - startTime)*0.001f;
  63.  
  64.             app.Update(deltaTime);
  65.             app.Render();
  66.  
  67.             startTime = t;
  68.         }
  69.     }
  70.  
  71.     app.Cleanup();
  72.  
  73.     return msg.wParam;
  74. }
  75.  
  76. APPLICATION::APPLICATION()
  77. {
  78.     m_pDevice = NULL; 
  79.     m_mainWindow = 0;
  80.     srand(GetTickCount());
  81. }
  82.  
  83. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  84. {
  85.     debug.Print("Application initiated");
  86.  
  87.     //Create Window Class
  88.     WNDCLASS wc;
  89.     memset(&wc, 0, sizeof(WNDCLASS));
  90.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  91.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  92.     wc.hInstance     = hInstance;
  93.     wc.lpszClassName = "D3DWND";
  94.  
  95.     //Register Class and Create new Window
  96.     RegisterClass(&wc);
  97.     m_mainWindow = CreateWindow("D3DWND", "Example 12.7: Constructing Buildings", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  98.     SetCursor(NULL);
  99.     ShowWindow(m_mainWindow, SW_SHOW);
  100.     UpdateWindow(m_mainWindow);
  101.  
  102.     //Create IDirect3D9 Interface
  103.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  104.  
  105.     if(d3d9 == NULL)
  106.     {
  107.         debug.Print("Direct3DCreate9() - FAILED");
  108.         return E_FAIL;
  109.     }
  110.  
  111.     //Check that the Device supports what we need from it
  112.     D3DCAPS9 caps;
  113.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  114.  
  115.     //Hardware Vertex Processing or not?
  116.     int vp = 0;
  117.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  118.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  119.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  120.  
  121.     //Check vertex & pixelshader versions
  122.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  123.     {
  124.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  125.     }
  126.  
  127.     //Set D3DPRESENT_PARAMETERS
  128.     D3DPRESENT_PARAMETERS d3dpp;
  129.     d3dpp.BackBufferWidth            = width;
  130.     d3dpp.BackBufferHeight           = height;
  131.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  132.     d3dpp.BackBufferCount            = 1;
  133.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  134.     d3dpp.MultiSampleQuality         = 0;
  135.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  136.     d3dpp.hDeviceWindow              = m_mainWindow;
  137.     d3dpp.Windowed                   = windowed;
  138.     d3dpp.EnableAutoDepthStencil     = true; 
  139.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  140.     d3dpp.Flags                      = 0;
  141.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  142.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  143.  
  144.     //Create the IDirect3DDevice9
  145.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  146.                                  vp, &d3dpp, &m_pDevice)))
  147.     {
  148.         debug.Print("Failed to create IDirect3DDevice9");
  149.         return E_FAIL;
  150.     }
  151.  
  152.     //Release IDirect3D9 interface
  153.     d3d9->Release();
  154.  
  155.     // Create m_light
  156.     ::ZeroMemory(&m_light, sizeof(m_light));
  157.     m_light.Type      = D3DLIGHT_DIRECTIONAL;
  158.     m_light.Ambient   = D3DXCOLOR(0.5, 0.5, 0.5, 1.0f);
  159.     m_light.Diffuse   = D3DXCOLOR(0.9, 0.9, 0.9, 1.0f);
  160.     m_light.Specular  = D3DXCOLOR(0.5, 0.5, 0.5, 1.0f);
  161.     D3DXVECTOR3 dir = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
  162.     D3DXVec3Normalize(&dir, &dir);
  163.     m_light.Direction = dir;    
  164.     m_pDevice->SetLight(0, &m_light);
  165.     m_pDevice->LightEnable(0, true);
  166.  
  167.     //Set sampler state
  168.     for(int i=0;i<4;i++)
  169.     {
  170.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  171.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  172.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  173.     }
  174.  
  175.     m_build1.Load("meshes/townhall_b.x", m_pDevice);
  176.     m_build2.Load("meshes/townhall.x", m_pDevice);
  177.     m_buildPrc = 0.0f;
  178.  
  179.     return S_OK;
  180. }
  181.  
  182. HRESULT APPLICATION::Update(float deltaTime)
  183. {
  184.     m_buildPrc += deltaTime * 0.07f;
  185.     if(m_buildPrc > 1.5f)m_buildPrc = 0.0f;
  186.  
  187.     //Keayboard input
  188.     if(KEYDOWN(VK_SPACE))
  189.     {
  190.     }
  191.     if(KEYDOWN(VK_RETURN))
  192.     {
  193.     }
  194.     else if(KEYDOWN(VK_ESCAPE))
  195.     {
  196.         Quit();
  197.     }
  198.  
  199.     return S_OK;
  200. }    
  201.  
  202. HRESULT APPLICATION::Render()
  203. {
  204.     // Clear the viewport
  205.     m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
  206.  
  207.     //Set camera
  208.     D3DXMATRIX view, proj, world, identity;
  209.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(15.0f, 30.0f, -40.0f), &D3DXVECTOR3(0.0f, 11.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  210.     D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.3f, 1.33333f, 0.01f, 1000.0f);
  211.     D3DXMatrixIdentity(&identity);
  212.  
  213.     m_pDevice->SetTransform(D3DTS_WORLD, &identity);
  214.     m_pDevice->SetTransform(D3DTS_VIEW, &view);
  215.     m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);
  216.  
  217.     // Begin the scene 
  218.     if(SUCCEEDED(m_pDevice->BeginScene()))
  219.     {
  220.         if(m_buildPrc < 1.0f)
  221.         {
  222.             m_build1.Render(m_buildPrc);
  223.  
  224.             //Progressbar
  225.             m_pDevice->Clear(1, &SetRect(10, 560, 790, 590), D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff000000, 1.0f, 0);
  226.             m_pDevice->Clear(1, &SetRect(12, 562, 788, 588), D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
  227.             m_pDevice->Clear(1, &SetRect(12, 562, 12 + 774 * m_buildPrc, 588), D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff00ff00, 1.0f, 0);
  228.         }
  229.         else m_build2.Render();        
  230.  
  231.         // End the scene.
  232.         m_pDevice->EndScene();
  233.         m_pDevice->Present(0, 0, 0, 0);
  234.     }
  235.  
  236.     return S_OK;
  237. }
  238.  
  239. HRESULT APPLICATION::Cleanup()
  240. {
  241.     try
  242.     {
  243.         m_build1.Release();
  244.         m_build2.Release();
  245.  
  246.         m_pDevice->Release();
  247.  
  248.         debug.Print("Application terminated");
  249.     }
  250.     catch(...){}
  251.  
  252.     return S_OK;
  253. }
  254.  
  255. HRESULT APPLICATION::Quit()
  256. {
  257.     ::DestroyWindow(m_mainWindow);
  258.     ::PostQuitMessage(0);
  259.     return S_OK;
  260. }